﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
    class UserDomain
    {
        public static double T_Mscore;
        public Dictionary<Document, int> m_myDocuments { get; set; } // SortedDictionary<docLength, docName>
        public UserDomain(double T_mscore)
        {
            T_Mscore = T_mscore;
            m_myDocuments = new Dictionary<Document, int>();
            m_myDocuments = m_myDocuments.OrderByDescending(x => x.Value).ToDictionary(x => x.Key, x => x.Value); //sorting by the length
        }

        /// <summary>
        /// the func recives two document and returns the percents of contents of the short doc in the longer one.
        /// </summary>
        /// <param name="d1"></param>
        /// <param name="d2"></param>
        /// <returns></returns>
        public double ContentsPercent(Document d1, Document d2)
        {
            double percentOfContents = 0;
            int occurrences = 0;
            if (d1.m_fingerPrints.Count >= d2.m_fingerPrints.Count) // d1 longer
            {
                foreach (int fingerprint in d2.m_fingerPrints.Keys)
                {
                    if (d1.m_fingerPrints.ContainsKey(fingerprint))
                        occurrences++;
                }
                percentOfContents = (double)occurrences / (double)d1.m_fingerPrints.Count;

            }
            else // d2 longer
            {
                foreach (int fingerprint in d1.m_fingerPrints.Keys)
                {
                    if (d2.m_fingerPrints.ContainsKey(fingerprint))
                        occurrences++;
                }
                percentOfContents = (double)occurrences / (double)d2.m_fingerPrints.Count;

            }
            percentOfContents = Math.Round(percentOfContents*100, 3);
            return percentOfContents;
        }

        public void ContentsPercent(Document d1)
        {
            double percentOfContents = 0;
            double max_Mscore = -1;

            int occurrences = 0;

            if (m_myDocuments.Count == 0)
                T_Mscore = 10; // need to be the mscore of the d1 - the first doc which checked.             

            else foreach (Document currDoc in m_myDocuments.Keys)
            {
                if (d1.m_fingerPrints.Count >= currDoc.m_fingerPrints.Count) // d1 longer
                {
                    foreach (int fingerprint in currDoc.m_fingerPrints.Keys)
                    {
                        if (d1.m_fingerPrints.ContainsKey(fingerprint))
                            occurrences++;
                    }
                    
                    percentOfContents = (double)occurrences / (double)d1.m_fingerPrints.Count;

                }
                else // d2 longer
                {
                    foreach (int fingerprint in d1.m_fingerPrints.Keys)
                    {
                        if (currDoc.m_fingerPrints.ContainsKey(fingerprint))
                            occurrences++;
                    }
                    percentOfContents = (double)occurrences / (double)currDoc.m_fingerPrints.Count;

                }
                if (percentOfContents > max_Mscore)
                    max_Mscore = percentOfContents;

                //tmp = percentOfContents;
                T_Mscore = 10; //TODO update the mscore
                
            }
            if (!m_myDocuments.ContainsKey(d1))
                m_myDocuments.Add(d1, d1.m_fingerPrints.Count);
            else
                Console.WriteLine("the user has already was expose to document: " + d1.m_Name);
            percentOfContents = Math.Round(percentOfContents * 100, 3);
            T_Mscore = percentOfContents * 1; //TODO calc the m_score based on the content that it contents.
        }
    }
}
